package com.aracky.assess.controller;

import com.aracky.assess.model.entity.HrKpiUserAssessorConfig;
import com.aracky.assess.model.form.HrKpiUserAssessorConfigForm;
import com.aracky.assess.model.request.HrKpiUserAssessorConfigRequest;
import com.aracky.assess.model.request.UserAndAssessorConfigDetailRequest;
import com.aracky.assess.model.vo.HrKpiUserAssessorConfigPageListVo;
import com.aracky.assess.model.vo.HrKpiUserAssessorConfigVO;
import com.aracky.assess.model.vo.UserAndAssessorConfigDetailVo;
import com.aracky.assess.service.HrKpiUserAssessorConfigService;
import com.aracky.common.util.ConvertUtil;
import com.aracky.common.vo.CommonResult;
import com.aracky.common.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import scala.util.parsing.input.Page;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 考核关系配置表 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2022-08-01 09:43:18
 */
@RestController
@RequestMapping("/hrKpiUserAssessorConfig")
@Api(tags = "用户评分人权重配置接口")
public class HrKpiUserAssessorConfigController {
    @Resource
    private HrKpiUserAssessorConfigService hrKpiUserAssessorConfigService;

    /**
     * 分页查询员工与考核关系详情信息
     * @param userAndAssessorConfigDetailRequest
     * @return
     */
    @PostMapping("/getPageUserAndAssessorConfigDetail")
    @ApiOperation(value = "分页查询出员工详情与考核关系详情")
    public CommonResult<PageVO<UserAndAssessorConfigDetailVo>> getPageUserAndAssessorConfigDetail(@RequestBody UserAndAssessorConfigDetailRequest userAndAssessorConfigDetailRequest){
        PageVO<UserAndAssessorConfigDetailVo> pageVo = hrKpiUserAssessorConfigService.getPageUserAndAssessorConfigDetail(userAndAssessorConfigDetailRequest);
        if (null == pageVo) {
            return CommonResult.error("查询失败！");
        }
        return CommonResult.success(pageVo,"查询成功！");
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存新增，更新考核关系配置信息")
    public CommonResult save(@RequestBody HrKpiUserAssessorConfigForm hrKpiUserAssessorConfigForm){
        HrKpiUserAssessorConfig hrKpiUserAssessorConfig = ConvertUtil.sourceToTarget(hrKpiUserAssessorConfigForm,HrKpiUserAssessorConfig.class);
        if (null == hrKpiUserAssessorConfig.getId()) {
            // 新增保存操作 (mybatis-plus新增不能获取新增操作人的姓名)
            if (hrKpiUserAssessorConfigService.save(hrKpiUserAssessorConfig)) {
                return CommonResult.success("新增保存成功!");
            }
            return CommonResult.error("新增保存失败!");
        }
        // mybatis-plus 修改失败
        // if (hrKpiUserAssessorConfigService.updateById(hrKpiUserAssessorConfig)) {
        //     return CommonResult.success("修改保存成功！");
        // }
        try {
            hrKpiUserAssessorConfigService.myUpdateById(hrKpiUserAssessorConfig);
        } catch (Exception e) {
            return CommonResult.error("修改保存失败！");
        }
        return CommonResult.success("修改保存成功！");
    }

    // /**
    //  * 分页查询出员工考核关系信息（byPostId,参数可扩展）
    //  * @param hrKpiUserAssessorConfigRequest
    //  * @return
    //  */
    // @PostMapping("/getPageList")
    // @ApiOperation(value = "分页查询出员工考核关系信息")
    // public CommonResult<PageVO<HrKpiUserAssessorConfigPageListVo>> getPageList(@RequestBody HrKpiUserAssessorConfigRequest hrKpiUserAssessorConfigRequest){
    //     PageVO<HrKpiUserAssessorConfigPageListVo> pageVo = hrKpiUserAssessorConfigService.getPageList(hrKpiUserAssessorConfigRequest);
    //     if (null == pageVo) {
    //         return CommonResult.error("查询失败!");
    //     }
    //     return CommonResult.success(pageVo,"查询成功！");
    // }
    // @PostMapping("/save")
    // @ApiOperation(value = "新增")
    // public CommonResult saveInfo(@RequestBody HrKpiUserAssessorConfigForm hrKpiUserAssessorConfigForm){
    //     HrKpiUserAssessorConfig hrKpiUserAssessorConfig= ConvertUtil.sourceToTarget(hrKpiUserAssessorConfigForm,HrKpiUserAssessorConfig.class);
    //     hrKpiUserAssessorConfigService.save(hrKpiUserAssessorConfig);
    //     return CommonResult.success("新增成功!");
    // }
    // @PutMapping("/update")
    // @ApiOperation(value = "修改")
    // public CommonResult updateInfo(@RequestBody HrKpiUserAssessorConfigForm hrKpiUserAssessorConfigForm){
    //     HrKpiUserAssessorConfig hrKpiUserAssessorConfig= ConvertUtil.sourceToTarget(hrKpiUserAssessorConfigForm,HrKpiUserAssessorConfig.class);
    //     hrKpiUserAssessorConfigService.updateById(hrKpiUserAssessorConfig);
    //     return CommonResult.success("修改成功!");
    // }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "ids",value = "表id")
    public CommonResult deleteInfo(Long[] ids){
        List<Long> list=new ArrayList<>();
        for (Long id:ids){
            list.add(id);
        }
        hrKpiUserAssessorConfigService.removeBatchByIds(list);
        return CommonResult.success("删除成功!");
    }

    // @GetMapping("/{id}")
    // @ApiOperation(value = "查看详情")
    // public CommonResult<HrKpiUserAssessorConfigVO> getInfo(@PathVariable Long id){
    //     HrKpiUserAssessorConfig hrKpiUserAssessorConfig=hrKpiUserAssessorConfigService.getById(id);
    //     HrKpiUserAssessorConfigVO hrKpiUserAssessorConfigVO=ConvertUtil.sourceToTarget(hrKpiUserAssessorConfig,HrKpiUserAssessorConfigVO.class);
    //     return CommonResult.success(hrKpiUserAssessorConfigVO,"查询详情成功!");
    // }

    // /**
    //  * 根据id查询员工考核关系信息
    //  * @param id
    //  * @return
    //  */
    // @GetMapping("/{id}")
    // @ApiOperation(value = "根据id查询员工考核关系信息")
    // public CommonResult<HrKpiUserAssessorConfigPageListVo> getById(@PathVariable Long id){
    //     HrKpiUserAssessorConfigPageListVo hrKpiUserAssessorConfigPageListVo = hrKpiUserAssessorConfigService.getMyById(id);
    //     if (null == hrKpiUserAssessorConfigPageListVo) {
    //         return CommonResult.error("查询失败!");
    //     }
    //     return CommonResult.success(hrKpiUserAssessorConfigPageListVo,"查询成功！");
    // }

    @GetMapping("/getByUserId")
    @ApiOperation(value = "根据userId查询员工考核关系信息")
    public CommonResult<HrKpiUserAssessorConfigPageListVo> getByUserId(@RequestParam("userId") Long userId){
        HrKpiUserAssessorConfigPageListVo hrKpiUserAssessorConfigPageListVo = hrKpiUserAssessorConfigService.getByUserId(userId);
        if (null == hrKpiUserAssessorConfigPageListVo) {
            return CommonResult.error("查询失败!");
        }
        return CommonResult.success(hrKpiUserAssessorConfigPageListVo,"查询成功！");
    }


    // @PostMapping("/pageList")
    // @ApiOperation(value = "列表查询")
    // public CommonResult<PageVO<HrKpiUserAssessorConfigVO>> list(@RequestBody HrKpiUserAssessorConfigRequest hrKpiUserAssessorConfigRequest){
    //     PageVO<HrKpiUserAssessorConfigVO> pageVO=hrKpiUserAssessorConfigService.getHrKpiUserAssessorConfigList(hrKpiUserAssessorConfigRequest);
    //     return CommonResult.success(pageVO);
    // }
}
